package com.huabo.contract.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.hbfk.util.PageInfo;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.TblContractPlannode;
import com.huabo.contract.oracle.entity.TblContractSpnode;
import com.huabo.contract.oracle.entity.TblCyhwUnit;
import com.huabo.contract.oracle.mapper.TblContractPlannodeMapper;
import com.huabo.contract.oracle.mapper.TblContractSpnodeMapper;
import com.huabo.contract.oracle.mapper.TblCyhwUnitMapper;
import com.huabo.contract.service.TblContractSpnodeService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class TblContractSpnodeServiceImpl implements TblContractSpnodeService {

    @Resource
    private TblContractPlannodeMapper tblContractPlannodeMapper;

    @Resource
    private TblCyhwUnitMapper tblCyhwUnitMapper;

    @Resource
    private TblContractSpnodeMapper tblContractSpnodeMapper;



    @Override
    public Map<String, Object> saveOrUpdateContractSpnode(TblContractSpnode spNode, Integer nodeId, Integer contractId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            TblCyhwUnit unit = this.tblCyhwUnitMapper.getCyhwUnitEntity(contractId);
            TblContractPlannode node = this.tblContractPlannodeMapper.findBynodeId(nodeId);
            if (spNode.getSpnodeid() != null) {
                spNode.setNodeid(node.getNodeid());
                spNode.setContractid(unit.getContractid());
                this.tblContractSpnodeMapper.updateBySpnode(spNode);
            } else {
                spNode.setNodeid(node.getNodeid());
                spNode.setContractid(unit.getContractid());
                this.tblContractSpnodeMapper.saveBySpnode(spNode);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "成功！");
            resultMap.put("data", spNode.getSpnodeid());
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            TblCyhwUnit unit = this.tblCyhwUnitMapper.getCyhwUnitEntity(contractId);
            TblContractPlannode node = this.tblContractPlannodeMapper.findBynodeId(nodeId);
            if (spNode.getSpnodeid() != null) {
                spNode.setNodeid(node.getNodeid());
                spNode.setContractid(unit.getContractid());
                this.tblContractSpnodeMapper.updateBySpnode(spNode);
            } else {
                spNode.setNodeid(node.getNodeid());
                spNode.setContractid(unit.getContractid());
                this.tblContractSpnodeMapper.saveBySpnode(spNode);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "成功！");
            resultMap.put("data", spNode.getSpnodeid());
            return resultMap;
        }

    }

    @Override
    public Map<String, Object> findListByXdf(PageInfo<TblContractSpnode> pageInfo, String budgetid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractSpnodeMapper.findListByXdf(pageInfo, budgetid));
            pageInfo.setTotalRecord(tblContractSpnodeMapper.findListByXdfCount(budgetid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractSpnodeMapper.findListByXdf(pageInfo, budgetid));
            pageInfo.setTotalRecord(tblContractSpnodeMapper.findListByXdfCount(budgetid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
            return resultMap;
        }
    }

    @Override
    public List<TblContractSpnode> findListByBudgetid(Integer budgetid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            //String sql = "SELECT * FROM TBL_CONTRACT_SPNODE WHERE ISWY='是' AND NODEPOST IS NOT NULL AND CONTRACTID IN (SELECT CONTRACTID FROM TBL_CYHW_UNIT WHERE CONTRACTXDFXINFO=" + budgetId + ")";
            List<TblContractSpnode> selectAllParentInfoList = this.tblContractSpnodeMapper.listBySql(budgetid);
            return selectAllParentInfoList;
        } else {
            //String sql = "SELECT * FROM TBL_CONTRACT_SPNODE WHERE ISWY='是' AND NODEPOST IS NOT NULL AND CONTRACTID IN (SELECT CONTRACTID FROM TBL_CYHW_UNIT WHERE CONTRACTXDFXINFO=" + budgetId + ")";
            List<TblContractSpnode> selectAllParentInfoList = this.tblContractSpnodeMapper.listBySql(budgetid);
            return selectAllParentInfoList;
        }
    }

    //8.合同执行登记接口
    //履行信息主键，为空新增，不为空修改
	@Override
	public Map<String, Object> setData(net.sf.json.JSONArray arr) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                TblContractSpnode spNode = null;
                for (Object j : arr) {
                    net.sf.json.JSONObject obj = (net.sf.json.JSONObject) j;
                    spNode = new TblContractSpnode();
                    spNode.setContractid(new BigDecimal(obj.get("contractId").toString()));
                    spNode.setStartdate(sdf.parse(obj.getString("startdate")));
                    spNode.setEnddate(sdf.parse(obj.getString("enddate")));
                    spNode.setNodememo(obj.getString("nodeMemo"));
                    spNode.setNodeid(new BigDecimal(obj.get("plannodeId").toString()));
                    if (!StringUtils.isEmpty(obj.getString("spNodeId"))) {
                        spNode.setSpnodeid(new BigDecimal(obj.get("spNodeId").toString()));
                    }
                    spNode.setNodecontent(obj.getString("spnodeContent"));
                    spNode.setIswy(obj.getString("isWy"));
                    spNode.setWyContent(obj.getString("wyContent"));
                    if (spNode.getSpnodeid() != null) {
                        this.tblContractSpnodeMapper.updateBySpnode(spNode);
                    } else {
                        this.tblContractSpnodeMapper.saveBySpnode(spNode);
                    }
                    if (spNode.getNodeid() != null) {
                        TblContractPlannode node = tblContractPlannodeMapper.findBynodeId(Integer.valueOf(obj.getString("plannodeId")));
                        node.setPlannodestatus(new BigDecimal("2"));
                        tblContractPlannodeMapper.updateContractPlannode(node);
                    }
                }
                resultMap.put("code", "0");
                resultMap.put("msg", "成功！");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                resultMap.put("code", "1");
                resultMap.put("msg", "失败！");
            }

            return resultMap;
        } else {
            // TODO Auto-generated method stub
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                TblContractSpnode spNode = null;
                for (Object j : arr) {
                    net.sf.json.JSONObject obj = (net.sf.json.JSONObject) j;
                    spNode = new TblContractSpnode();
                    spNode.setContractid(new BigDecimal(obj.get("contractId").toString()));
                    spNode.setStartdate(sdf.parse(obj.getString("startdate")));
                    spNode.setEnddate(sdf.parse(obj.getString("enddate")));
                    spNode.setNodememo(obj.getString("nodeMemo"));
                    spNode.setNodeid(new BigDecimal(obj.get("plannodeId").toString()));
                    if (!StringUtils.isEmpty(obj.getString("spNodeId"))) {
                        spNode.setSpnodeid(new BigDecimal(obj.get("spNodeId").toString()));
                    }
                    spNode.setNodecontent(obj.getString("spnodeContent"));
                    spNode.setIswy(obj.getString("isWy"));
                    spNode.setWyContent(obj.getString("wyContent"));
                    if (spNode.getSpnodeid() != null) {
                        this.tblContractSpnodeMapper.updateBySpnode(spNode);
                    } else {
                        this.tblContractSpnodeMapper.saveBySpnode(spNode);
                    }
                    if (spNode.getNodeid() != null) {
                        TblContractPlannode node = tblContractPlannodeMapper.findBynodeId(Integer.valueOf(obj.getString("plannodeId")));
                        node.setPlannodestatus(new BigDecimal("2"));
                        tblContractPlannodeMapper.updateContractPlannode(node);
                    }
                }
                resultMap.put("code", "0");
                resultMap.put("msg", "成功！");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                resultMap.put("code", "1");
                resultMap.put("msg", "失败！");
            }

            return resultMap;
        }
	}
}
